<template>
	<view>
		<view class="wrap">
			<view class="wrap-list">
				<block v-for="(item, index) in list" :key="index">
					<view class="item">
						<view class="choose" @click="onChoose(index)">
							<image v-if="item.select" src="@/static/live/choose_select.png"></image>
							<image v-else src="@/static/live/choose_normal.png"></image>
						</view>
						
						<image class="file" :src="item.image"></image>
						
						<view class="right">
							<view class="name">{{item.title}}</view>
							
							<view class="price-bg">
								<text class="symbol">¥</text>
								<text>{{item.price}}</text>
							</view>
						</view>
					</view>
				</block>
			</view>
		</view>
		
		<view class="footer">
			<view class="sure" @click.stop="onSure">确定</view>
		</view>
	</view>
</template>

<script>
	import {
		zbGoodsList
	} from "@/API/newLive.js";
	import st from "@/utils/showtoast.js";
	export default {
		data() {
			return {
				page: 1,
				total: 0,
				list: [],
				goodsIDList: []
			}
		},
		onLoad(options) {
			if (options.goodsList) {
				let goodsList = JSON.parse(options.goodsList)
				for (let i = 0; i < goodsList.length; i++) {
					this.goodsIDList.push(goodsList[i].id)
				}
			}
			this.onRefresh()
		},
		onPullDownRefresh() {
			this.onRefresh()
		},
		onReachBottom() {
			if (this.list.length < this.total) {
				this.page++
				this.getData()
			}
		},
		methods: {
			/**
			 * 选择
			 */
			onChoose(index) {
				this.list[index].select = !this.list[index].select
				
				this.$set(this.list)
			},
			/**
			 * 确定
			 */
			onSure() {
				let chooseArray = []
				for (let i = 0; i < this.list.length; i++) {
					let item = this.list[i]
					if (item.select == true) {
						chooseArray.push(item)
					}
				}
				if (chooseArray.length == 0) {
					st.show("请选择要带货的商品")
					return;
				}
				uni.$emit("chooseFinishGoods", chooseArray)
				uni.navigateBack()
			},
			
			/**
			 * 刷新
			 */
			onRefresh() {
				this.page = 1
				this.list = []
				this.getData()
			},
			/**
			 * 网络请求
			 */
			async getData() {
				let res = await zbGoodsList(this.page)
				this.total = res.data.total
				for (let i = 0; i < res.data.data.length; i++) {
					if (this.goodsIDList.indexOf(res.data.data[i].id) == -1) {
						res.data.data[i]["select"] = false
					} else {
						res.data.data[i]["select"] = true
					}
				}
				if (this.page == 1) {
					this.list = res.data.data
				} else {
					this.list = this.list.concat(res.data.data)
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	page {
		background-color: #F6F6F6;
	}
	.wrap {
		padding-bottom: calc(120rpx + env(safe-area-inset-bottom));
		width: 100%;
		overflow: hidden;
		
		.wrap-list {
			width: 100%;
			padding: 0 20rpx;
			overflow: hidden;
			
			.item {
				width: 100%;
				padding: 20rpx 0;
				background-color: white;
				border-radius: 20rpx;
				margin-bottom: 20rpx;
				display: flex;
				align-items: center;
				
				.choose {
					width: 60rpx;
					height: 60rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					
					>image {
						width: 100%;
						height: 100%;
					}
				}
				
				.file {
					margin-left: 10rpx;
					width: 180rpx;
					height: 180rpx;
					border-radius: 10rpx;
				}
				
				.right {
					margin-left: 10rpx;
					flex: 1;
					min-height: 180rpx;
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					overflow: hidden;
					
					.name {
						color: #333;
						font-size: 28rpx;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 2;
						overflow: hidden;
						text-overflow: ellipsis;
					}
					
					.price-bg {
						color: red;
						font-size: 40rpx;
						display: flex;
						align-items: baseline;
						
						.symbol {
							font-size: 28rpx;
							margin-right: 5rpx;
						}
					}
				}
			}
		}
	}
	
	
	
	.footer {
		position: fixed;
		left: 0;
		bottom: 0;
		width: 100%;
		height: 120rpx;
		padding-bottom: env(safe-area-inset-bottom);
		z-index: 10;
		background-color: white;
		display: flex;
		align-items: center;
		justify-content: center;
		box-sizing: content-box;
		
		.sure {
			width: 680rpx;
			height: 100rpx;
			line-height: 100rpx;
			text-align: center;
			color: white;
			border-radius: 50rpx;
			overflow: hidden;
			background-color: $theme-color;
		}
	}
</style>